package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.StudentBean;
import com.project.mapper.IStudentMapper;
import com.project.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;

@Service
@Transactional
public class StudentServiceImpl implements IStudentService {
    @Autowired
    private IStudentMapper mapper;

    @Override
    public void add(StudentBean student) {
        mapper.insert(student);
    }

    @Override
    public void del(Integer id) {
        mapper.deleteById(id);
    }

    @Override
    public void update(Integer id, String phone) {
        StudentBean student = mapper.selectById(id);
        student.setPhone(phone);
        mapper.updateById(student);
    }

    @Override
    public StudentBean findById(Integer id) {
        return mapper.selectById(id);
    }

    @Override
    public IPage<StudentBean> findByItem(Integer pageNO, String name, LocalDate startDate, LocalDate endDate) {
        QueryWrapper<StudentBean> qw = new QueryWrapper<>();
        if(name != null && name.length() != 0){
            qw.like("s_name",name);
        }
        if(startDate != null){
            qw.ge("s_birthday",startDate);
        }
        if(endDate != null){
            qw.le("s_birthday",endDate);
        }

        return mapper.selectPage(new Page(pageNO,3),qw);
    }
}
